<?php
namespace app\index\model;
use think\Model;
use think\Db;
use think\Exception;
class Agency extends Model
{
    protected $resultSetType = 'collection';
    public function showall(){
        $db = Db::name('agency')->where('state',1)->select();
        return $db;
    }
    /*修改代理商数据*/
    public function agencymod($id,$shop,$name,$iphone,$email,$region,$shi,$qu,$jwd){
        try {
            //开启事务
            Db::startTrans();
            $db1 = Db::name('agency')
                ->update([
                    'id' => $id,
                    'shop' => $shop,
                    'name' => $name,
                    'iphone' => $iphone,
                    'email' => $email,
                    'region' => $region,
                    'location' => $jwd
                ]);
            $db2 = Db::name('shopregion')
                ->where('agencyid', $id)
                ->update([
                    'shi' => $shi,
                    'qu' => $qu
                ]);
            if (!($db1 !== false && $db2 !== false)) {
                throw new Exception('出现意外');
            }
            Db::commit();
            return 1;
        }
        catch (Exception $e) {
            //如获取到异常信息，对所有表操作，都会回滚至操作前的状态：
            Db::rollback();
            return 0;
        }
    }

    /*代理商添加*/
    public function agencyadd($shop,$name,$iphone,$email,$jwd,$shi,$qu,$region){
        try {
            //开启事务
            Db::startTrans();
            $user = Db::name('agency')->insertGetId([
                'shop' => $shop,
                'name' => $name,
                'iphone' => $iphone,
                'email' => $email,
                'location' => $jwd,
                'region' => $region,
                'prefix' => $shop,
                'state'  => 1
            ]);
            $shiqu = Db::name('shopregion')->insert([
                'shi' => $shi,
                'qu' => $qu,
                'agencyid' => $user
            ]);
            if (!($user !== false && $shiqu !== false)) {
                throw new Exception('出现意外');
            }
            Db::commit();
            return 1;
        }
        catch (Exception $e) {
            //如获取到异常信息，对所有表操作，都会回滚至操作前的状态：
            Db::rollback();
            return 0;
        }
    }
    public function agencydel($id){
        try {
            //开启事务
            Db::startTrans();
            $delagent = Db::name('agency')->delete($id);
            $delregion = Db::name('shopregion')->where('agencyid',$id)->delete();
            if (!($delagent !== false && $delregion !== false)) {
                throw new Exception('出现意外');
            }
            Db::commit();
            return 1;
        }
        catch (Exception $e) {
            //如获取到异常信息，对所有表操作，都会回滚至操作前的状态：
            Db::rollback();
            return 0;
        }
    }
    //清空业务员管理的所有店铺
    public function Delbusiness($id){
        $res=$this->save(['adminid'=>'0','manageid'=>'0'],['adminid'=>$id]);
        return $res;
    }
    //清空团长管理的所有店铺
    public function Delcolonel($id){
        $res=$this->save(['manageid'=>'0'],['manageid'=>$id]);
        return $res;
    }
    //划分店铺到团长名下
    public function addcolonel($manageid,$id){
        $res=$this->save(['manageid'=>$manageid],['id'=>['in',$id]]);
        return $res;
    }
    public function showaudit($state){
        $db = Db::name('agency as a')->where('state',$state)
            ->join('businesscertificate b','b.agencyid=a.id','left')
            ->field('a.id,a.shop,a.name,a.iphone,a.state,a.reject,b.idcardfront,b.idcardreverse,b.businesslicense,b.storefronta,b.storefrontb')
            ->select();
        for($i=0;$i<count($db);$i++){
            if($db[$i]['idcardfront']==null){
                $db[$i]['idcardfront']="1.jpg";
            }
            if($db[$i]['idcardreverse']==null){
                $db[$i]['idcardreverse']="1.jpg";
            }
            if($db[$i]['businesslicense']==null){
                $db[$i]['businesslicense']="1.jpg";
            }
            if($db[$i]['storefronta']==null){
                $db[$i]['storefronta']="1.jpg";
            }
            if($db[$i]['storefrontb']==null){
                $db[$i]['storefrontb']="1.jpg";
            }
        }
        return $db;
    }
    public function agree($id){
        $db = Db::name('agency')
            ->update([
                'id'    => $id,
                'state' =>1,
                'times' =>date('Y-m-d H:i:s')
            ]);
        if($db){
            return 1;
        }else{
            return 0;
        }
    }
    public function disagree($id,$reason){
        $db = Db::name('agency')
            ->update([
                'id'    => $id,
                'state' =>2,
                'reject' => $reason
            ]);
        if($db){
            return 1;
        }else{
            return 0;
        }
    }
    //每个业务员下的团长管理的店铺数量查询
    public function colonelAll($id,$isadmin){
        if($isadmin==1)
            $where['ismanage']=1;
        else
            $where=['ismanage'=>'1',"b.adminid"=>$id];
        $res=$this->alias('a')
            ->join("user","a.manageid=user.id",'right')
            ->join("useradmin b","b.userid=user.id",'left')
            ->join("admin","admin.id=b.adminid",'left')
            ->where($where)
            ->group("user.id")
            ->order('admin.id')
            ->field('user.name,admin.account as admin,count(manageid) as count,user.id,b.adminid')
            ->select();
        return $res;
    }
    //查询业务员对应的所有店铺id
    public function AdminShopId($where){
        $res = db('agency')->where($where)->field('id')->select();
        $ids=[];
        foreach ($res as $v)
            $ids[]=$v['id'];
        return $ids;
    }
    //取消驳回，返回至待审核
    public function abolish($id){
        $db = Db::name('agency')
            ->update([
                'id'    => $id,
                'state' =>0,
                'reject' => null
            ]);
        if($db){
            return 1;
        }else{
            return 0;
        }

    }
    public function applyforwithdrawal($withdrawal){
        $db = Db::name('agency a')->where('withdrawal',$withdrawal)->where('state',1)
            ->join('user u','a.id = u.agencyid','left')
            ->field('a.id,a.shop,a.name,a.iphone,count(u.agencyid) people,a.refused')
            ->group('a.id')
            ->select();
        return $db;
    }
    public function agreewithdrawal($id){
        $db = Db::name('agency')
            ->update([
               'id' => $id,
                'withdrawal' => 2,
                'refused' => null
            ]);
        return $db;
    }
    public function disagreewithdrawal($id,$reason){
        $db = Db::name('agency')
            ->update([
                'id' => $id,
                'withdrawal' => 3,
                'refused' => $reason
            ]);
        return $db;
    }
    //取消驳回提现申请，返回至待审核
    public function abolish2($id){
        $db = Db::name('agency')
            ->update([
                'id'    => $id,
                'withdrawal' =>1,
                'refused' => null
            ]);
        if($db){
            return 1;
        }else{
            return 0;
        }

    }
}